using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Configuration;
using System.Diagnostics;
using System.Collections;
using System.Collections.Specialized;
using System.Threading;
using System.Configuration;


using System.Net;
using System.Net.Sockets;
using System.Data.OleDb;
using System.Data;
using System.Text.RegularExpressions;
using System.Data.SqlClient;
using System.Runtime.Serialization;
using System.Linq;
using System.Xml.XPath;
using System.Xml.Linq;



namespace CLoggerNamespace
{
	public class CLogger
	{
		private static int _ALL = 100;
		private static int _INFO = 75;
		private static int _ERROR = 50;
		private static int _OFF = 0;
		private static int _COMM = 40;
		private static String strLogDirectory = "\\";
		private static String strLogPrefix = "HTSwitchService";

		public static void Init()
		{
			NameValueCollection appSettings = ConfigurationManager.AppSettings;

			foreach (String strEntry in appSettings.AllKeys)
			{
				switch (strEntry)
				{
					case "logDirectory":
						strLogDirectory = appSettings[strEntry];
						break;
					case "logPrefix":
						strLogPrefix = appSettings[strEntry];
						break;
				}
			}
		}

		public static string LogPrefix
		{
			set { strLogPrefix = value; }
		}

		public static int ALL
		{
			get { return _ALL; }
		}
		public static int ERROR
		{
			get { return _ERROR; }
		}
		public static int INFO
		{
			get { return _INFO; }
		}
		public static int OFF
		{
			get { return _OFF; }
		}
		public static int COMM
		{
			get { return _COMM; }
		}

		

		private static string LogLevelToString(int level)
		{
			string strLogLevel;
			switch (level)
			{
				case 100: // _ALL
					strLogLevel = "ALL  ";
					break;
				case 50: // _ERROR
					strLogLevel = "ERROR";
					break;
				case 75: // _INFO
					strLogLevel = "INFO ";
					break;
				case 40: // _COMM
					strLogLevel = "COMM ";
					break;
				case 0: // _OFF
				default:
					strLogLevel = "OFF  ";
					break;
			}
			return strLogLevel;
		}

		public static void Append(String message, int level)
		{
			StackTrace stackTrace;
			String strMethodName;
			int logLevel = _OFF;

			stackTrace = new System.Diagnostics.StackTrace();
			strMethodName = stackTrace.GetFrame(1).GetMethod().Name;

			String strLogLevel = ConfigurationManager.AppSettings["logLevel"].ToString();
			switch (strLogLevel)
			{
				case "ALL":
					logLevel = _ALL;
					break;
				case "ERROR":
					logLevel = _ERROR;
					break;
				case "INFO":
					logLevel = _INFO;
					break;
				default:
					logLevel = _OFF;
					break;
			}
			if (logLevel >= level)
			{
                Object thisLock = new Object();
                lock (thisLock)
                {
                    try
                    {
                        DateTime dt = DateTime.Now;
                        String filePath = strLogDirectory + "\\" + strLogPrefix + dt.ToString("yyyyMMdd") + ".log";
                        if (!File.Exists(filePath))
                        {
                            FileStream fs = File.Create(filePath);
                            fs.Close();
                        }

                        try
                        {
                            StreamWriter sw = File.AppendText(filePath);
                            sw.WriteLine(dt.ToString("HH:mm:ss") + "|" + "Thread ID: " + Thread.CurrentThread.ManagedThreadId.ToString() + "|" + LogLevelToString(level) + "|" + strMethodName + "|" + message);
                            sw.Flush();
                            sw.Close();
                            sw.Dispose();
                            sw = null;
                        }
                        catch /*( Exception ex ) */
                        {
                            //					Console.WriteLine(ex.Message.ToString());
                        }
                    }
                    catch
                    {
                    }
                }
			}
		}
	}
}
